<?php

class urlteaController extends PicoraController {
    public function home(){
		return $this->render('views/pages/home.php');
    }

    public function create(){
		$url = trim(@$this->params['url']);
		if ($url != "") {
			$shorturl = new shortURL();
			$shorturl->generate($url, $this->getRequestMethod());
	        return $this->render('views/pages/urlmade.php', array(
	            'shorturl' => $shorturl
	        ));
		} else {
			// no url so redirect
	        return $this->redirect('home', false);
		}		
    }

	public function redirectFromKeycode($keycode){
		$shorturl = new shortURL();
		$newurl = $shorturl->getURL($keycode);
		if ($newurl != "") {
			//echo $url;
			header("HTTP/1.1 307 Temporary Redirect");
			header("Location: ".$newurl);
			exit();
		} else {
			$this->flash('message',"Curious... A redirect for <br />" . BASE_URL . $keycode . "<br />has yet to be created.");
			return $this->redirect('home', false);
		}
	}
	
	public function about() {
        return $this->render('views/pages/about.php', false);
	}
	
	public function apidocs () {
        return $this->render('views/pages/apidocs.php', false);
	}
	public function textapi () {
		$url = trim(@$this->params['url']);
		if ($url != "") {
			$shorturl = new shortURL();
			$newurl = $shorturl->generate($url, 'api-text ' . @$_REQUEST['apikey']);
			echo $newurl;
		}
		exit();
	}
	public function jsonapi () {
		$url = trim(@$this->params['url']);
		if ($url != "") {
			$shorturl = new shortURL();
			$newurl = $shorturl->generate($url, 'api-text ' . @$_REQUEST['apikey']);
			$json = "urltea = {url:'".$newurl."', keycode: '".$shorturl->keycode."', error: '".$shorturl->error."', message: '".$shorturl->message."'};";
			echo $json;
		}
		exit();
	}
	
}

?>